package com.itheima.project.mainPage;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.itheima.project.R;

import java.util.List;


public class ViewPageAdapter extends PagerAdapter {

    private Context mContext;
    private List<ViewPageEntity.DataBean> mList;

    public ViewPageAdapter(Context mContext, List<ViewPageEntity.DataBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.viewpage_item, null, false);
        ImageView ivImage = inflate.findViewById(R.id.iv_image_viewpage_item);
        String imagePath = mList.get(position%mList.size()).getImagePath();
        Glide.with(mContext).load(imagePath).into(ivImage);
        container.addView(inflate);
        return inflate;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}